const app = getApp();
const utils = require('../../../utils/util.js');
const sharePost = require('../../../utils/sharePost.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imagePath: '',
    cardinfo: {},
    couponList: {},
    showMode: 0, //0首页个人样式，1首页商家样式
    canvHeight: 0,
    stylemer: 9, //商家样式1，商家样式2, 商家样式3，商家样式4：样式顺序：9、8、7、1、2、3、4、5、6
    isHolder: 0,
    holderCanvHeight: 0,
    windowWidth: 375, //手机屏幕宽度
    windowWidth2: 375,
    heightStyle: 496,
    avatar: '',
    codeSrc: '',
    bgPath: '',//上传的海报背景图
    showCanvasImg: ''//要保存的海报样式（全路径）
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取屏幕宽度px
    let that = this;
    var windowWidth = wx.getSystemInfoSync().windowWidth * 0.96;

    var windowWidth2 = wx.getSystemInfoSync().windowWidth * 0.88;
    var heightStyle = windowWidth * 1.5;

    var cardinfo = JSON.parse(that.decodeUnicode((options.cardinfo)));
    that.data.cardinfo = cardinfo;
    that.setData({
      windowWidth: windowWidth,
      windowWidth2: windowWidth2,
      heightStyle: heightStyle
    })
    if (options.showmode && options.stylemer) {
      that.data.stylemer = options.stylemer;
      that.setData({
        showMode: options.showmode
      })
    }
    if (options.showmode == 0) { //个人名片版
      that.getAvaterInfo(cardinfo);
    }
    if (options.showmode > 0) { //商家版
      that.drawPoste();
    }
    if (options.isHolder == 1) { //0张560、1张800,2张976,3张及以上1156
      that.setData({
        isHolder: options.isHolder
      })
      var userinfo = JSON.parse(that.decodeUnicode(options.userinfo));
      var couponList = JSON.parse(that.decodeUnicode(options.couponList));
      let holderCanvHeight = 280;
      if (couponList.length == 0) {
        holderCanvHeight = windowWidth / 0.96 * 280 / 375;
        that.setData({
          holderCanvHeight: holderCanvHeight
        })
      }
      if (couponList.length == 1) {
        holderCanvHeight = windowWidth / 0.96 * 400 / 375
        that.setData({
          holderCanvHeight: holderCanvHeight
        })
      }
      if (couponList.length == 2) {
        holderCanvHeight = windowWidth / 0.96 * 488 / 375
        that.setData({
          holderCanvHeight: holderCanvHeight
        })
      }
      if (couponList.length > 2) {
        holderCanvHeight = windowWidth / 0.96 * 578 / 375
        that.setData({
          holderCanvHeight: holderCanvHeight
        })
      }
      that.getCanvasimgHolder(cardinfo, userinfo, couponList);
    }
  },

  // 解码
  decodeUnicode: function (str) {
    str = str.replace(/\\/g, "%");
    return unescape(str);
  },

  // 获取海报
  drawPoste: function (bgPath = '') {
    let that = this;
    let cardinfo = that.data.cardinfo;
    wx.request({
      url: app.globalData.host + '/api/tool/CreateIndexPoster?uid=' + cardinfo.CardInfo.Uid + '&bgPath=' + bgPath,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data) {
            let showCanvasImg = utils.formatAvater(res.data.Data, 'https://static.711688.net.cn/upload/card/source/');
            that.setData({
              bgPath: res.data.Data,
              showCanvasImg: showCanvasImg
            })
          }

        }else{
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 保存商家版海报
  saveMerImg: function () {
    let that = this;
    if (that.data.showCanvasImg) {
      wx.downloadFile({
        url: that.data.showCanvasImg,
        success: function (res) {
          console.log(res);
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success(res) {
              utils.aiCardActionRecord(19);
              wx.showModal({
                content: '图片已保存到相册，赶紧晒一下吧~',
                showCancel: false,
                confirmText: '好的',
                confirmColor: '#333',
                success: function (res) {
                  if (res.confirm) { }
                },
                fail: function (res) { }
              })
            },
            fail: function (res) {
              wx.showToast({
                title: '请删掉小程序再重新授权允许保存至相册',
                icon: 'none',
                duration: 2000
              })
            }
          })
        }, fail: function (err) {
          wx.showToast({
            title: '图片下载失败',
            mask: true
          })
        }
      })
    } else {
      wx.showToast({
        title: '保存失败',
        mask: true
      })
    }
  },

  upLoadBgimg: function () {
    let that = this;
    var pics = [];
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        var imgs = res.tempFilePaths;
        pics.push(imgs[0]);
        that.uploadimg({
          url: app.globalData.host + '/api/tool/upload?operation=savecardavater', //这里是你图片上传的接口
          path: pics, //这里是选取的图片的地址数组
        });
      },
    })
  },
 
  //多张图片上传（照片墙）
  uploadimg: function (data) {
    wx.showLoading({
      title: '提交中...',
      mask: true,
    })
    var that = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'fileData',
      formData: null,
      success: (resp) => {
        wx.hideLoading();
        success++;
        var str = resp.data
        var pic = JSON.parse(str);
        var pic_name = pic.Data;
        that.data.bgPath = pic_name;
        that.drawPoste(pic_name);
      },
      fail: (res) => {
        fail++;
      },
      complete: (cp) => {
        i++;
        if (i == data.path.length) { //当图片传完时，停止调用     
          console.log('执行完毕');
          console.log('成功：' + success + " 失败：" + fail);
        } else { //若图片还没有传完，则继续调用函数
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }
      }
    });
  },

  // 股东海报
  // 下载图片（股东样式）
  getCanvasimgHolder: function (cardinfo, userinfo, couponList) {
    var that = this;
    wx.downloadFile({ //限时特惠图标
      url: 'https://static.711688.net.cn/upload/store/68/blog/thumb500_800/s_1904240848243645092.jpg',
      success: function (res) {
        let canvasimg = res.tempFilePath;
        that.getCanvasimgTopred(cardinfo, userinfo, couponList, canvasimg);
      }
    })
  },
  getCanvasimgTopred: function (cardinfo, userinfo, couponList, canvasimg) {
    var that = this;
    wx.downloadFile({
      url: 'https://static.711688.net.cn/upload/store/68/blog/thumb500_800/s_1905071046352394165.png',
      success: function (res) {
        let canvasimg2 = res.tempFilePath; //顶部红包
        that.getQrCodeHolder(cardinfo, userinfo, couponList, canvasimg, canvasimg2);
      }
    })
  },
  //下载股东海报二维码
  getQrCodeHolder: function (cardInfo, userinfo, couponList, canvasimg, canvasimg2) {
    wx.showLoading({
      title: '生成中...',
      mask: true,
    });
    var that = this;
    // let path = '/pages/index/index?holderid=' + app.globalData.userInfo.Uid + '&id=' + cardInfo.CardInfo.Id + '&holderName=' + app.globalData.userInfo.NickName;
    let path = 'pages/index/index?t=3&p=' + cardInfo.CardInfo.Id + '#' + app.globalData.userInfo.Uid;
    wx.request({
      url: app.globalData.host + "/api/Store/GetWxaCodeFile",
      data: {
        storeId: app.globalData.storeId,
        path: path,
        storeappid: app.globalData.storeAppId
      },
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data) {
            wx.downloadFile({
              url: 'https://static.711688.net.cn/upload/user/' + res.data.Data,
              success: function (res) {
                wx.hideLoading();
                if (res.statusCode === 200) {
                  var codeSrc = res.tempFilePath;
                  that.sharePosteCanvasHolder(cardInfo, userinfo, couponList, codeSrc, canvasimg, canvasimg2);
                } else {
                  wx.showToast({
                    title: '二维码下载失败！',
                    icon: 'none',
                    duration: 2000,
                    success: function () {
                      var codeSrc = "";
                      that.sharePosteCanvasHolder(cardInfo, userinfo, couponList, codeSrc, canvasimg, canvasimg2);
                    }
                  })
                }
              }
            })
          } else {
            wx.hideLoading()
            var codeSrc = "";
            that.sharePosteCanvasHolder(cardInfo, userinfo, couponList, codeSrc, canvasimg, canvasimg2);
          }
        }
      },
    })
  },

  //canvas绘制分享海报（股东礼包海报）
  sharePosteCanvasHolder: function (cardInfo, userinfo, couponList, codeSrc, canvasimg, canvasimg2) {
    wx.showLoading({
      title: '生成中...',
      mask: true,
    })
    var that = this;
    const ctx = wx.createCanvasContext('holderCanvas');
    wx.createSelectorQuery().select('#canvas-holder').boundingClientRect(function (rect) {
      var height = rect.height;
      var width = rect.width;
      //优惠券及小程序码部分的高度
      var postHeight = width * 0.96 * 509 / 360;

      ctx.fillStyle = '#EDEEF2';
      ctx.fillRect(0, 0, width, height);

      // 顶部红包
      if (canvasimg2) {
        ctx.fillStyle = '#fff';
        ctx.fillRect(0, 0, width, width * 0.16);
        ctx.drawImage(canvasimg2, width * 21 / 375, (width * 0.16 - 32) / 2, 28, 32);
      }
      if (userinfo) {
        ctx.setTextAlign('left');
        ctx.fillStyle = 'rgba(55, 55, 55, 1)';
        ctx.font = '16px sans-serif';
        let title = "老板" + userinfo.NickName + "给您赠送了红包";
        ctx.fillText(title, width * 57 / 375, width * 12 / 125);
      }
      ctx.fillStyle = '#fff';
      ctx.fillRect(width * 0.02, width * 0.16 + 8, width * 0.96, postHeight);
      if (couponList.length > 0) {
        //红色框(三张优惠券高度：290；两张：195， 一张：105)
        if (couponList.length == 1) {
          sharePost.fillRoundRect(ctx, width * 0.96 / 18, width * 0.16 + 19, width * 335 / 375, postHeight * 105 / 509, 10, 'rgba(230, 35, 17, 1)');
        }
        if (couponList.length == 2) {
          sharePost.fillRoundRect(ctx, width * 0.96 / 18, width * 0.16 + 19, width * 335 / 375, postHeight * 195 / 509, 10, 'rgba(230, 35, 17, 1)');
        }

        if (couponList.length > 2) {
          sharePost.fillRoundRect(ctx, width * 0.96 / 18, width * 0.16 + 19, width * 335 / 375, postHeight * 290 / 509, 10, 'rgba(230, 35, 17, 1)');
        }
        // 第一张优惠券
        ctx.fillStyle = 'rgba(253,236,206,1)';
        ctx.fillRect(10, 10, width * 335 / 375 - 20, postHeight * 70 / 509);
        ctx.fillStyle = '#FFF';
        ctx.fillRect(10, width * 0.8 * 7 / 30, width * 335 / 375 - 20, postHeight * 23 / 509);
        if (couponList[0].name != '' && couponList[0].money == 0) {
          let title = couponList[0].name;
          ctx.font = '15px sans-serif';
          ctx.fillStyle = 'rgba(224,32,15,1)';
          ctx.setTextAlign('left');
          //优惠券名称
          ctx.fillText(title, 20, postHeight * 32 / 509, width * 0.8 - 20);
        }
        if (couponList[0].extcode1 != '' && couponList[0].extcode2 != '') {
          let title = '买' + couponList[0].extcode1 + '送' + couponList[0].extcode2;
          ctx.font = '15px sans-serif';
          ctx.fillStyle = 'rgba(224,32,15,1)';
          ctx.setTextAlign('left');
          //优惠券名称
          ctx.fillText(title, 20, postHeight * 32 / 509, width * 0.8 - 20);
        }
        if (couponList[0].extcode1 == '' && couponList[0].extcode2 != '') {
          let title = '送' + couponList[0].extcode2;
          ctx.font = '15px sans-serif';
          ctx.fillStyle = 'rgba(224,32,15,1)';
          ctx.setTextAlign('left');
          //优惠券名称
          ctx.fillText(title, 20, postHeight * 32 / 509, width * 0.8 - 20);
        }
        if (couponList[0].money > 0) {
          let title = '¥' + couponList[0].money
          ctx.font = '15px sans-serif';
          ctx.fillStyle = 'rgba(224,32,15,1)';
          ctx.setTextAlign('left');
          //优惠券名称
          ctx.fillText(title, 20, postHeight * 32 / 509, width * 0.8 - 20);
          let orderamountlower = '满' + couponList[0].orderamountlower + '使用';
          ctx.font = '8px sans-serif';
          ctx.fillStyle = 'rgba(224,32,15,1)';
          ctx.setTextAlign('left');
          //满多少可使用
          ctx.fillText(orderamountlower, 20, postHeight * 50 / 509, width * 0.8 - 20);
        }
        if (couponList[0].useendtime) {
          let useendtime = '';
          if (couponList[0].useexpiretime > 0) {
            useendtime = '有效期：' + couponList[0].useexpiretime + '天';
          } else {
            useendtime = '有效期：' + couponList[0].usestarttime + '至' + couponList[0].useendtime;
          }
          ctx.font = '8px sans-serif';
          ctx.fillStyle = 'rgba(224,32,15,1)';
          ctx.setTextAlign('left');
          //有效期
          ctx.fillText(useendtime, 20, postHeight * 62 / 509, width * 0.8 - 20);
        }
        if (couponList[0].Company) {
          ctx.font = '8px sans-serif';
          ctx.fillStyle = 'rgba(224,32,15,1)';
          ctx.setTextAlign('left');
          //公司名
          ctx.fillText(couponList[0].Company, 20, postHeight * 85 / 509, width * 0.8 - 20);
        }

        if (couponList.length > 1) {
          // 第二张优惠券
          ctx.fillStyle = 'rgba(253,236,206,1)';
          ctx.fillRect(10, postHeight * 103 / 509, width * 335 / 375 - 20, postHeight * 70 / 509);
          ctx.fillStyle = '#FFF';
          ctx.fillRect(10, postHeight * 163 / 509, width * 335 / 375 - 20, postHeight * 23 / 509);
          if (couponList[1].name != '' && couponList[1].money == 0) {
            let title = couponList[1].name;
            ctx.font = '15px sans-serif';
            ctx.fillStyle = 'rgba(224,32,15,1)';
            ctx.setTextAlign('left');
            //优惠券名称
            ctx.fillText(title, 20, postHeight * 128 / 509, width * 0.8 - 20);
          }
          if (couponList[1].extcode1 != '' && couponList[1].extcode2 != '') {
            let title = '买' + couponList[1].extcode1 + '送' + couponList[1].extcode2;
            ctx.font = '15px sans-serif';
            ctx.fillStyle = 'rgba(224,32,15,1)';
            ctx.setTextAlign('left');
            //优惠券名称
            ctx.fillText(title, 20, postHeight * 128 / 509, width * 0.8 - 20);
          }
          if (couponList[1].extcode1 == '' && couponList[1].extcode2 != '') {
            let title = '送' + couponList[1].extcode2;
            ctx.font = '15px sans-serif';
            ctx.fillStyle = 'rgba(224,32,15,1)';
            ctx.setTextAlign('left');
            //优惠券名称
            ctx.fillText(title, 20, postHeight * 128 / 509, width * 0.8 - 20);
          }
          if (couponList[1].money > 0) {
            let title = '¥' + couponList[1].money
            ctx.font = '15px sans-serif';
            ctx.fillStyle = 'rgba(224,32,15,1)';
            ctx.setTextAlign('left');
            //优惠券名称
            ctx.fillText(title, 20, postHeight * 128 / 509, width * 0.8 - 20);
            let orderamountlower = '满' + couponList[1].orderamountlower + '使用';
            ctx.font = '8px sans-serif';
            ctx.fillStyle = 'rgba(224,32,15,1)';
            ctx.setTextAlign('left');
            //满多少可使用
            ctx.fillText(orderamountlower, 20, postHeight * 142 / 509, width * 0.8 - 20);
          }
          if (couponList[1].useendtime) {
            let useendtime = '';
            if (couponList[1].useexpiretime > 0) {
              useendtime = '有效期：' + couponList[1].useexpiretime + '天';
            } else {
              useendtime = '有效期：' + couponList[1].usestarttime + '至' + couponList[1].useendtime;
            }
            ctx.font = '8px sans-serif';
            ctx.fillStyle = 'rgba(224,32,15,1)';
            ctx.setTextAlign('left');
            //有效期
            ctx.fillText(useendtime, 20, postHeight * 154 / 509, width * 0.8 - 20);
          }
          if (couponList[1].Company) {
            ctx.font = '8px sans-serif';
            ctx.fillStyle = 'rgba(224,32,15,1)';
            ctx.setTextAlign('left');
            //公司名
            ctx.fillText(couponList[1].Company, 20, postHeight * 177 / 509, width * 0.8 - 20);
          }
        }

        if (couponList.length > 2) {
          // 第三张优惠券
          ctx.fillStyle = 'rgba(253,236,206,1)';
          ctx.fillRect(10, postHeight * 195 / 509, width * 335 / 375 - 20, postHeight * 70 / 509);
          ctx.fillStyle = '#FFF';
          ctx.fillRect(10, postHeight * 255 / 509, width * 335 / 375 - 20, postHeight * 23 / 509);
          if (couponList[2].name != '' && couponList[2].money == 0) {
            let title = couponList[2].name;
            ctx.font = '15px sans-serif';
            ctx.fillStyle = 'rgba(224,32,15,1)';
            ctx.setTextAlign('left');
            //优惠券名称
            ctx.fillText(title, 20, postHeight * 216 / 509, width * 0.8 - 20);
          }
          if (couponList[2].extcode1 != '' && couponList[2].extcode2 != '') {
            let title = '买' + couponList[2].extcode1 + '送' + couponList[2].extcode2;
            ctx.font = '15px sans-serif';
            ctx.fillStyle = 'rgba(224,32,15,1)';
            ctx.setTextAlign('left');
            //优惠券名称
            ctx.fillText(title, 20, postHeight * 216 / 509, width * 0.8 - 20);
          }
          if (couponList[2].extcode1 == '' && couponList[2].extcode2 != '') {
            let title = '送' + couponList[2].extcode2;
            ctx.font = '15px sans-serif';
            ctx.fillStyle = 'rgba(224,32,15,1)';
            ctx.setTextAlign('left');
            //优惠券名称
            ctx.fillText(title, 20, postHeight * 216 / 509, width * 0.8 - 20);
          }
          if (couponList[2].money > 0) {
            let title = '¥' + couponList[2].money
            ctx.font = '15px sans-serif';
            ctx.fillStyle = 'rgba(224,32,15,1)';
            ctx.setTextAlign('left');
            //优惠券名称
            ctx.fillText(title, 20, postHeight * 216 / 509, width * 0.8 - 20);
            let orderamountlower = '满' + couponList[2].orderamountlower + '使用';
            ctx.font = '8px sans-serif';
            ctx.fillStyle = 'rgba(224,32,15,1)';
            ctx.setTextAlign('left');
            //满多少可使用
            ctx.fillText(orderamountlower, 20, postHeight * 234 / 509, width * 0.8 - 20);
          }
          if (couponList[2].useendtime) {
            let useendtime = '';
            if (couponList[2].useexpiretime > 0) {
              useendtime = '有效期：' + couponList[2].useexpiretime + '天';
            } else {
              useendtime = '有效期：' + couponList[2].usestarttime + '至' + couponList[2].useendtime;
            }
            ctx.font = '8px sans-serif';
            ctx.fillStyle = 'rgba(224,32,15,1)';
            ctx.setTextAlign('left');
            //有效期
            ctx.fillText(useendtime, 20, postHeight * 246 / 509, width * 0.8 - 20);
          }
          if (couponList[2].Company) {
            ctx.font = '8px sans-serif';
            ctx.fillStyle = 'rgba(224,32,15,1)';
            ctx.setTextAlign('left');
            //公司名
            ctx.fillText(couponList[2].Company, 20, postHeight * 270 / 509, width * 0.8 - 20);
          }
        }
      }

      if (couponList.length == 0) {
        // 绘制二维码
        if (codeSrc) {
          ctx.drawImage(codeSrc, 35, 105, width * 107 / 375, width * 107 / 375);
        }
        if (cardInfo.CardInfo.Company) {
          // 公司名称
          ctx.setTextAlign('left');
          ctx.fillStyle = 'rgba(255, 132, 39, 1)';
          ctx.font = '20px sans-serif';
          ctx.fillText(cardInfo.CardInfo.Company, width * 107 / 375 + 70, 140, 150);
        }
        if (canvasimg) {
          ctx.drawImage(canvasimg, width * 107 / 375 + 70, 160, 132, 31);
        }
        ctx.setTextAlign('center');
        ctx.fillStyle = 'rgba(55, 55, 55, 1)';
        ctx.font = '11px sans-serif';
        ctx.fillText('企动·玖玖客', width * 0.48, 245);
      }

      // 一张
      //扫码送大礼
      if (couponList.length == 1) {
        ctx.moveTo(0, postHeight * 135 / 509);
        ctx.lineTo(width * 110 / 375, postHeight * 135 / 509);
        ctx.setStrokeStyle('rgba(255,132,39,1)')
        ctx.stroke()
        ctx.font = '20px sans-serif';
        ctx.fillStyle = 'rgba(255,132,39,1)';
        ctx.fillText('扫码领券', width * 110 / 375 + 9, 142);
        ctx.moveTo(width * 110 / 375 + 99, postHeight * 135 / 509);
        ctx.lineTo(width * 220 / 375 + 99, postHeight * 135 / 509);
        ctx.setStrokeStyle('rgba(255,132,39,1)');
        ctx.stroke();

        // 绘制二维码
        if (codeSrc) {
          ctx.drawImage(codeSrc, 5, postHeight * 135 / 509 + 30, width * 107 / 375, width * 107 / 375);
        }
        if (cardInfo.CardInfo.Company) {
          // 公司名称
          ctx.setTextAlign('left');
          ctx.fillStyle = 'rgba(255, 132, 39, 1)';
          ctx.font = '20px sans-serif';
          ctx.fillText(cardInfo.CardInfo.Company, width * 107 / 375 + 20, postHeight * 135 / 509 + 60, 150);
        }
        if (canvasimg) {
          ctx.drawImage(canvasimg, width * 107 / 375 + 20, postHeight * 220 / 509, 132, 31);
        }
        ctx.setTextAlign('center');
        ctx.fillStyle = 'rgba(55, 55, 55, 1)';
        ctx.font = '11px sans-serif';
        ctx.fillText('企动·玖玖客', width * 0.4, postHeight * 295 / 509);
      }

      //扫码送大礼
      if (couponList.length == 2) {
        ctx.moveTo(0, postHeight * 230 / 509);
        ctx.lineTo(width * 110 / 375, postHeight * 230 / 509);
        ctx.setStrokeStyle('rgba(255,132,39,1)')
        ctx.stroke()
        ctx.font = '20px sans-serif';
        ctx.fillStyle = 'rgba(255,132,39,1)';
        ctx.fillText('扫码领券', width * 110 / 375 + 9, postHeight * 237 / 509);
        ctx.moveTo(width * 110 / 375 + 99, postHeight * 230 / 509);
        ctx.lineTo(width * 220 / 375 + 99, postHeight * 230 / 509);
        ctx.setStrokeStyle('rgba(255,132,39,1)');
        ctx.stroke();

        // 绘制二维码
        if (codeSrc) {
          ctx.drawImage(codeSrc, 5, postHeight * 260 / 509, width * 107 / 375, width * 107 / 375);
        }
        if (cardInfo.CardInfo.Company) {
          // 公司名称
          ctx.setTextAlign('left');
          ctx.fillStyle = 'rgba(255, 132, 39, 1)';
          ctx.font = '20px sans-serif';
          ctx.fillText(cardInfo.CardInfo.Company, width * 107 / 375 + 20, postHeight * 260 / 509 + 30, 150);
        }
        if (canvasimg) {
          ctx.drawImage(canvasimg, width * 107 / 375 + 20, postHeight * 260 / 509 + 50, 132, 31);
        }
        ctx.setTextAlign('center');
        ctx.fillStyle = 'rgba(55, 55, 55, 1)';
        ctx.font = '11px sans-serif';
        ctx.fillText('企动·玖玖客', width * 0.4, postHeight * 380 / 509);
      }

      //扫码送大礼(3张及以上券)
      if (couponList.length > 2) {
        ctx.moveTo(0, postHeight * 315 / 509 + 5);
        ctx.lineTo(width * 110 / 375, postHeight * 315 / 509 + 5);
        ctx.setStrokeStyle('rgba(255,132,39,1)')
        ctx.stroke()
        ctx.font = '20px sans-serif';
        ctx.fillStyle = 'rgba(255,132,39,1)';
        ctx.fillText('扫码领券', width * 110 / 375 + 9, postHeight * 322 / 509 + 5);
        ctx.moveTo(width * 110 / 375 + 99, postHeight * 315 / 509 + 5);
        ctx.lineTo(width * 220 / 375 + 99, postHeight * 315 / 509 + 5);
        ctx.setStrokeStyle('rgba(255,132,39,1)');
        ctx.stroke();

        // 绘制二维码
        if (codeSrc) {
          ctx.drawImage(codeSrc, 5, postHeight * 345 / 509 + 5, width * 107 / 375, width * 107 / 375);
        }
        if (cardInfo.CardInfo.Company) {
          // 公司名称
          ctx.setTextAlign('left');
          ctx.fillStyle = 'rgba(255, 132, 39, 1)';
          ctx.font = '20px sans-serif';
          ctx.fillText(cardInfo.CardInfo.Company, width * 107 / 375 + 20, postHeight * 345 / 509 + 35, 150);
        }
        if (canvasimg) {
          ctx.drawImage(canvasimg, width * 107 / 375 + 20, postHeight * 345 / 509 + 55, 132, 31);
        }
        ctx.setTextAlign('center');
        ctx.fillStyle = 'rgba(55, 55, 55, 1)';
        ctx.font = '11px sans-serif';
        ctx.fillText('企动·玖玖客', width * 0.45, postHeight * 470 / 509);
      }

    }).exec()
    setTimeout(function () {
      ctx.draw();
      wx.hideLoading();
    }, 1000)
  },

  // 个人版首页转发海报
  //下载头像
  getAvaterInfo: function (cardInfo) {
    var that = this;
    if (cardInfo.CardInfo.Avater) {
      let imgUrl = utils.formatAvater(cardInfo.CardInfo.Avater, 'https://static.711688.net.cn/upload/card/source/');
      wx.downloadFile({
        url: imgUrl,
        success: function (res) {
          if (res.statusCode === 200) {
            var avaterSrc = res.tempFilePath;
            that.getQrCodePersion(avaterSrc, cardInfo);
          } else {
            wx.showToast({
              title: '头像下载失败！',
              icon: 'none',
              duration: 2000,
              success: function () {
                var avaterSrc = "";
                that.getQrCodePersion(avaterSrc, cardInfo);
              }
            })
          }
        }
      })
    } else {
      var avaterSrc = "";
      that.getQrCodePersion(avaterSrc, cardInfo);
    }
  },

  //下载二维码
  getQrCodePersion: function (avaterSrc, cardInfo) {
    wx.showLoading({
      title: '生成中...',
      mask: true,
    });
    var that = this;
    if (cardInfo.CardInfo.QrCode) {
      wx.downloadFile({
        url: cardInfo.CardInfo.QrCode,
        success: function (res) {
          wx.hideLoading();
          if (res.statusCode === 200) {
            var codeSrc = res.tempFilePath;
            that.sharePosteCanvas(cardInfo, avaterSrc, codeSrc);
          } else {
            wx.showToast({
              title: '二维码下载失败！',
              icon: 'none',
              duration: 2000,
              success: function () {
                var codeSrc = "";
                that.sharePosteCanvas(cardInfo, avaterSrc, codeSrc);
              }
            })
          }
        }
      })
    } else {
      wx.hideLoading()
      var codeSrc = "";
      that.sharePosteCanvas(cardInfo, avaterSrc, codeSrc);
    }
  },

  //canvas绘制分享海报（首页个人版）
  sharePosteCanvas: function (cardInfo, avaterSrc, codeSrc) {
    wx.showLoading({
      title: '生成中...',
      mask: true,
    })
    const ctx = wx.createCanvasContext('myCanvas');
    var width = "";
    wx.createSelectorQuery().select('#canvas-container').boundingClientRect(function (rect) {
      var height = rect.height;
      width = rect.width * 0.8;
      var left = rect.left + 5;
      ctx.fillStyle = '#fff';
      ctx.fillRect(0, 0, rect.width, height);

      //头像
      if (avaterSrc) {
        ctx.drawImage(avaterSrc, left, 20, width, width);
        ctx.font = '14px sans-serif';
        ctx.fillStyle = '#fff';
        ctx.setTextAlign('left');
      }

      if (cardInfo.CardInfo.Name) {
        ctx.font = '14px sans-serif';
        ctx.fillStyle = '#000';
        ctx.setTextAlign('left');
        ctx.fillText(cardInfo.CardInfo.Name, left, width + 60); //姓名
      }

      if (cardInfo.CardInfo.Position) {
        ctx.font = '12px sans-serif';
        ctx.fillStyle = '#666';
        ctx.setTextAlign('left');
        ctx.fillText(cardInfo.CardInfo.Position, left, width + 85); //职位
      }

      if (cardInfo.CardInfo.Mobile) {
        ctx.font = '12px sans-serif';
        ctx.fillStyle = '#666';
        ctx.setTextAlign('left');
        ctx.fillText(cardInfo.CardInfo.Mobile, left, width + 105); //电话
      }

      if (cardInfo.CardInfo.Company) {
        // 公司名称
        const CONTENT_ROW_LENGTH = 24; // 正文 单行显示字符长度
        let [contentLeng, contentArray, contentRows] = sharePost.textByteLength(cardInfo.CardInfo.Company, CONTENT_ROW_LENGTH);
        ctx.setTextAlign('left');
        ctx.fillStyle = '#000';
        ctx.font = '10px sans-serif';
        let contentHh = 22 * 1;
        for (let m = 0; m < contentArray.length; m++) {
          ctx.fillText(contentArray[m], left, width + 150 + contentHh * m);
        }
      }
      //  绘制二维码cardInfo.CardInfo.QrCode
      if (codeSrc) {
        ctx.drawImage(codeSrc, left + 160, width + 40, width / 3, width / 3)
        ctx.font = '10px sans-serif';
        ctx.fillStyle = '#000';
        ctx.fillText("微信扫码或长按识别", left + 160, width + 150);
      }

    }).exec()

    setTimeout(function () {
      ctx.draw();
      wx.hideLoading();
    }, 1000)

  },

  //点击保存到相册(个人版首页转发海报)
  saveShareImg: function () {
    wx.showLoading({
      title: '正在保存',
      mask: true,
    })
    setTimeout(function () {
      wx.canvasToTempFilePath({
        canvasId: 'myCanvas',
        success: function (res) {
          wx.hideLoading();
          var tempFilePath = res.tempFilePath;
          wx.saveImageToPhotosAlbum({
            filePath: tempFilePath,
            success(res) {
              utils.aiCardActionRecord(19);
              wx.showModal({
                content: '图片已保存到相册，赶紧晒一下吧~',
                showCancel: false,
                confirmText: '好的',
                confirmColor: '#333',
                success: function (res) {
                  if (res.confirm) { }
                },
                fail: function (res) { }
              })
            },
            fail: function (res) {
              wx.showToast({
                title: '请删掉小程序再重新授权允许保存至相册',
                icon: 'none',
                duration: 2000
              })
            }
          })
        }
      });
    }, 1000);
  },

  //点击保存到相册（股东礼包海报）
  saveHolderPost: function () {
    wx.showLoading({
      title: '正在保存',
      mask: true,
    })
    setTimeout(function () {
      wx.canvasToTempFilePath({
        canvasId: 'holderCanvas',
        success: function (res) {
          wx.hideLoading();
          var tempFilePath = res.tempFilePath;
          wx.saveImageToPhotosAlbum({
            filePath: tempFilePath,
            success(res) {
              utils.aiCardActionRecord(19);
              wx.showModal({
                content: '图片已保存到相册，赶紧晒一下吧~',
                showCancel: false,
                confirmText: '好的',
                confirmColor: '#333',
                success: function (res) {
                  if (res.confirm) { }
                },
                fail: function (res) { }
              })
            },
            fail: function (res) {
              wx.showToast({
                title: '请删掉小程序再重新授权允许保存至相册',
                icon: 'none',
                duration: 2000
              })
            }
          })
        }
      });
    }, 1000);
  }
})